Retrieve Static Array from DxlObject

Hello,

I have been trying to put a static array into a DxlObject (as a reference) and get it back later.
The putting is standard, it was the getting I was troubled by.

I think I have it solved now, and would like to share and get confirmation that I am doing it correctly!

Usually, you need to cast the datatype on retrieval, but I don't see how to do that with arrays.
Instead, I define the variable to be a referenced array (of strings in this case), and don't cast at all.
 

void PrintStringArray(string sArray[])
{
    int iArrayIndex
        for(iArrayIndex = 0; iArrayIndex < sizeof(sArray); iArrayIndex+=1) {
                string sElement = sArray[iArrayIndex]
                print "sElement : " sElement "\n"
        }
}
 
void AnotherFunction(DxlObject aDxlObjectArg)
{
        print "\nAnotherFunction\n"
        
        int iNumber2 = (aDxlObjectArg->"iNumber") int
        print "iNumber2 : " iNumber2 "\n"
        
        string (&sNumbersArray1)[] = (aDxlObjectArg->"sStaticArray")
        PrintStringArray(sNumbersArray1)
        
        print "\nAnotherFunction - After Change\n"
        sNumbersArray1[0] = "zero"
        PrintStringArray(sNumbersArray1)
        
}
 
void Main()
{
        print "Main\n"
        
        int iNumber1 = 123
        print "iNumber1 : " iNumber1 "\n"
        
        string sNumbersArray1[] = {"one", "two", "three"}
        PrintStringArray(sNumbersArray1)
        
        DxlObject aDxlObject = new()
        aDxlObject->"iNumber" = iNumber1
        aDxlObject->"sStaticArray" = sNumbersArray1
        AnotherFunction(aDxlObject)
        delete(aDxlObject)
        
        print "\nMain - After AnotherFunction\n"
        PrintStringArray(sNumbersArray1)
}
 
Main()

 


As usual with doing this sort of thing, you must be very carful about when variables go out of scope and are mangled by subsequent stack changes.
In this case we are ok because the array is created low on the stack and will still be there in the 'AnotherFunction' function.
You can't create a static array in the 'AnotherFunction' function, put it in the DxlObject and expect to be able to get it back after the function returns to the 'Main' function.

Adam

 


SystemAdmin - Tue Jul 10 06:53:46 EDT 2012

Re: Retrieve Static Array from DxlObject
Mathias Mamsch - Tue Jul 10 08:05:55 EDT 2012

Looks fine, however, why would you not want to marshal the array to an 'Array' and recreate it later. Would be much better for a library, and the risk of stack trashing would gone. I think we had similar posts in the past, discussing this. Regards, Mathias

Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS